﻿/* jQuery selectpicker
 * version: 1.0
 *王柏林
 */
!function ($) {
    var selectpicker = function (element, setting) {
        if (setting.data.data) {
            bind(setting.data.data);
        }
        else {
            var parm;
            if (typeof (setting.data.param) == "function") {
                parm = setting.data.param();
            } else parm = setting.data.param;

            $.request(setting.data.url, parm, function (result) {
                bind(result);
                if (setting.finish) {
                    setting.finish();
                }
            }, false, false);
        }

        function bind(result) {

            var ccultuer = "zh-CN";
            if (culture)
                ccultuer = culture.getCulture();
            var $this = element;
            $this.empty();
            var options = '';
            switch (setting.data.type) {
                case "all":
                    options += "<option value=''>" + (ccultuer == 'zh-CN' ? '全部' : 'All') + "</option>";
                    break;
                case "none":
                    options += "<option value=''></option>";
                    break;
                case "first":

                    break;
                case "select":
                    options += "<option value=''>" + (ccultuer == 'zh-CN' ? '请选择' : 'Please Select') + "</option>";
                    break;

                default:
                    break;
            }
            if (options != '') $this.append(options);
            if (result) {

                $.each(result, function (i, value) {
                    var st = false;
                    for (var h = 0; h < setting.exdata.length; h++) {
                        if (setting.exdata[h] == value[setting.data.key.id]) {
                            st = true;
                        }
                    }
                    if (!st) {
                        var myValue = "";
                        if (value[setting.data.key.enname] == undefined)
                            myValue = value[setting.data.key.name];
                        else
                            myValue = ccultuer == "zh-CN" ? value[setting.data.key.name] :
                                value[setting.data.key.enname];
                        var option = $("<option>").attr('value', value[setting.data.key.id]).html(myValue);
                        $this.append(option);
                    }
                });
                if (typeof(setting.callback) == 'function') {
                    setting.callback();
                }
            }

        }

    };
    $.fn.selectpicker = function (setting) {
        if (!setting.data.param)
            setting.data.param = {};
        setting.data.param.token = context.token;
        setting.data.param.parentId = setting.data.parentId;
        new selectpicker(this, $.extend(true, {}, $.fn.selectpicker.defaults, setting));
    };
    $.fn.selectpicker.defaults = {
        data: {
            type: '',
            url: '/common/dictionary/query/parentId',
            param: {token: context.token},
            key: {
                id: 'id',
                name: "name",
                enname: 'enName'
            }
        },
        callback: {
            //  beforeClick: beforeClick,
            //   onClick: onClick
        },
        exdata: [-1]
    };
}(window.jQuery);